package com.xb.cloud.product.category.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xb.cloud.common.model.tree.TreeNode;
import com.xb.cloud.common.model.tree.TreeUtils;
import com.xb.cloud.common.util.BeanUtils;
import com.xb.cloud.product.category.service.client.CategoryTreeService;
import com.xb.cloud.product.category.service.client.bo.CategoryTreesBO;
import com.xb.cloud.product.merchant.service.client.bo.ShopNavigationGroupBo;
import com.xb.cloud.product.service.business.model.CategoryTrees;
import com.xb.cloud.product.service.business.model.CategoryTreesExample;
import com.xb.cloud.product.service.business.model.ShopNavigationGroup;
import com.xb.cloud.product.service.business.model.CategoryTreesExample.Criteria;
import com.xb.cloud.product.service.business.model.mapper.CategoryTreesMapper;
import com.xb.cloud.service.client.ServiceReturnCodes;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;

/**
 * 
 * @ClassName: CategoryTreeServiceImpl
 * @Description:TODO
 * @author: 鲜备科技
 * @date: 2018年1月8日 下午4:34:30
 * 
 * @Copyright: 2018 www.xianbeitech.com Inc. All rights reserved.
 *
 */
@Service("categoryTreeService")
public class CategoryTreeServiceImpl implements CategoryTreeService {
	@Resource
	CategoryTreesMapper categoryTreesMapper;

	@Override
	public ServiceReturnInfo getFrontCategory(long companyID) {
		CategoryTreesExample example = new CategoryTreesExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsDeletedEqualTo((byte) 0);
		criteria.andCompanyIdEqualTo(companyID);
		criteria.andCategoryTreeTypeEqualTo((byte) 5);
		example.setOrderByClause("category_sort asc");
		List<CategoryTrees> categoryTreeList = null;
		try {
			categoryTreeList = categoryTreesMapper.selectByExample(example);
		} catch (Exception e) {
			return ServiceReturnUtils.error(ServiceReturnCodes.PRODUCT_SQL_EXCEPTION);
		}
		List<CategoryTreesBO> bos = new ArrayList<CategoryTreesBO>();
		for (CategoryTrees categoryTrees : categoryTreeList) {
			CategoryTreesBO bo = new CategoryTreesBO();
			BeanUtils.copyProperties(categoryTrees, bo);
			bos.add(bo);
		}
		return ServiceReturnUtils.success(bos);
	}

	@Override
	public ServiceReturnInfo getNavCategory(long companyId) {
		CategoryTreesExample example = new CategoryTreesExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsDeletedEqualTo((byte) 0);
		criteria.andCompanyIdEqualTo(companyId);
		criteria.andCategoryTreeTypeEqualTo((byte) 1);
		List<CategoryTrees> categoryTreeList = null;
		try {
			categoryTreeList = categoryTreesMapper.selectByExample(example);
		} catch (Exception e) {
			return ServiceReturnUtils.error(ServiceReturnCodes.PRODUCT_SQL_EXCEPTION);
		}

		// 通用列表转换成树形列表
		List<TreeNode<CategoryTreesBO>> treeNodeList = convertToTreeNodeList(categoryTreeList);

		// 使用工具类构建树形结构
		TreeNode<CategoryTreesBO> tree = TreeUtils.buildTree(treeNodeList);
		return ServiceReturnUtils.success(tree);
	}

	/**
	 * 将店铺内分组pojo列表，转换为通用树节点列表
	 * 
	 * @param navigationGroupList
	 * @return
	 */
	private List<TreeNode<CategoryTreesBO>> convertToTreeNodeList(List<CategoryTrees> navigationGroupList) {

		List<TreeNode<CategoryTreesBO>> treeNodeList = new ArrayList<TreeNode<CategoryTreesBO>>();
		for (CategoryTrees categoryTrees : navigationGroupList) {
			TreeNode<CategoryTreesBO> treeNode = new TreeNode<CategoryTreesBO>();
			treeNode.setId(categoryTrees.getId());
			treeNode.setCode(categoryTrees.getCategoryCode());
			treeNode.setIsLeaf(categoryTrees.getCategoryIsLeaf());
			treeNode.setName(categoryTrees.getCategoryName());
			treeNode.setParentId(categoryTrees.getCategoryParentId());
			treeNode.setSort(categoryTrees.getCategorySort());
			// 设置data
			treeNode.setData(BeanUtils.newAndCopyProperties(CategoryTreesBO.class, categoryTrees));
			treeNodeList.add(treeNode);
		}
		return treeNodeList;

	}
	/**
	 * 根据id列表获取categorytree表中的数据
	 * <p>Title: getCategoryTreeByIDs</p>   
	 * <p>Description: </p>   
	 * @param categoryIDs
	 * @return   
	 * @see com.xb.cloud.product.category.service.client.CategoryTreeService#getCategoryTreeByIDs(java.util.List)
	 */
	@Override
	public ServiceReturnInfo getCategoryTreeByIDs(List<Long> categoryIDs) {
		CategoryTreesExample example = new CategoryTreesExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdIn(categoryIDs);
		List<CategoryTrees> categoryTreesList = categoryTreesMapper.selectByExample(example);
		List<CategoryTreesBO> boList = new ArrayList<CategoryTreesBO>();
		for (CategoryTrees categoryTrees : categoryTreesList) {
			boList.add(BeanUtils.newAndCopyProperties(CategoryTreesBO.class, categoryTrees));
		}
		//TODO 异常处理
		return ServiceReturnUtils.success(boList);
	}
}
